<?php
/**
 * 计划任务用 业务执行时间配置
 * Created by PhpStorm.
 * User: 亦喆
 * Date: 2022/9/30
 * Time: 11:35
 */

namespace App\Services\Task;

use App\Models\TaskBusinessControlModel;

class TaskBusinessControlOperate {

    /**
     * 获取开始结束时间、起始id
     * @return array|int
     */
    public static function getLogAndStart($params = []) {
        $options = [
            'taskBusinessType' => 0,
            'nowTime' => time(), //当前时间，时间锚点
            'timeoutMax' => 60, //超时时间
            'stepTime' => 60, //每次执行时间步长
        ];
        if (is_array($params) && !empty($params)) {
            $options = array_merge($options, $params);
        }
        extract($options);

        $nowTime = (int)$nowTime; //时间锚点
        $nowDate = date('Y-m-d H:i:s', $nowTime);
        $timeoutMax = (int)$timeoutMax; //超时时间
        $stepTime = (int)$stepTime; //每次执行时间步长
        //获取上次执行最后的id
        $logIdRes = TaskBusinessControlModel::query()->lock()
            ->select()
            ->where('business_type', $taskBusinessType)
            ->first();
        $taskStartDate = date('Y-m-d H:i:s', $nowTime - 60 * 10);
        $taskEndDate = $nowDate;
        if($logIdRes) {
            $logIdRes = $logIdRes->toArray();
            if($logIdRes['running_state']) {
                if(($nowTime - strtotime($logIdRes['end_date'])) <= $timeoutMax) {
                    return [
                        'skip' => 1,
                    ];
                }
                $logIdRes['end_date'] = $taskStartDate;
            }
            //计算单次处理结束时间
            $taskEndTime = strtotime($logIdRes['end_date']) + $stepTime;
            if($taskEndTime > $nowTime) { //结束时间大于当前时间，使用当前时间
                $taskEndTime = $nowTime;
            }
            $taskStartDate = $logIdRes['end_date'];
            $taskEndDate = date('Y-m-d H:i:s', $taskEndTime);
        }
        TaskBusinessControlModel::query() //置为执行中状态
        ->select(['business_id'])
            ->where('business_type', $taskBusinessType)
            ->update([
                'running_state' => 1
            ]);
        //执行开始时间前推一小时
        //        $taskStartDate = date('Y-m-d H:i:s', strtotime($taskStartDate) - 60 * 60);
        return [
            'skip' => 0,
            'taskStartDate' => $taskStartDate,
            'taskEndDate' => $taskEndDate,
        ];
    }

    /**
     * 执行完毕，保存执行log
     * @param array $params
     * @return int
     */
    public static function finish($params = []) {
        $options = [
            'taskBusinessType' => 0,
            'taskEndDate' => '',
            'taskStartDate' => '',
        ];
        if (is_array($params) && !empty($params)) {
            $options = array_merge($options, $params);
        }
        extract($options);

        if(!$taskBusinessType || !$taskEndDate || !$taskStartDate) {
            return false;
        }

        //重新获取当前执行记录
        $logIdRes = TaskBusinessControlModel::query()->lock()
            ->select()
            ->where('business_type', $taskBusinessType)
            ->first();
        if($logIdRes) {
            $logIdRes = $logIdRes->toArray();
            //若当前进程的执行结束时间小于log表的结束时间，则代表本进程可能超时，不覆盖log
            if($logIdRes['end_date'] >= $taskEndDate) {
                $taskEndDate = $logIdRes['end_date'];
                $taskStartDate = $logIdRes['start_date'];
            }
        }
        $upData = [
            'start_date'    => $taskStartDate,
            'end_date'      => $taskEndDate,
            'running_state' => 0
        ];
        //获取上次执行最后的id
        $logIdRes = TaskBusinessControlModel::query()
            ->select(['business_id'])
            ->where('business_type', $taskBusinessType)
            ->update($upData);

        return $logIdRes;
    }


}